import 'package:e_book/model/book.dart';
import 'package:e_book/components/book_tile/my_book_tile.dart';
import 'package:e_book/components/my_search_tile.dart';
import 'package:e_book/pages/douban_store/douban_store_vm.dart';
import 'package:e_book/utils/navigator_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';

class DoubanStorePage extends StatefulWidget {
  const DoubanStorePage({super.key});

  @override
  State<DoubanStorePage> createState() => _DoubanStorePageState();
}

class _DoubanStorePageState extends State<DoubanStorePage> {
  final DubanStoreViewModel _viewModel = DubanStoreViewModel();

  @override
  void initState() {
    super.initState();
    // 初始化数据
    _viewModel.getDoubanStoreData();
  }

  @override
  Widget build(BuildContext context) {
    Color surf = Theme.of(context).colorScheme.surface;
    return ChangeNotifierProvider.value(
      value: _viewModel,
      builder: (context, child) {
        return Scaffold(
          appBar: AppBar(
            elevation: 0,
            toolbarHeight: 0,
            backgroundColor: surf,
            surfaceTintColor: surf,
          ),
          backgroundColor: surf,
          body: _getBodyUI(),
        );
      },
    );
  }
}

Widget _getBodyUI() {
  return SafeArea(
      child: SingleChildScrollView(
    padding: EdgeInsets.only(right: 15.w, top: 12.w, left: 15.w),
    child: Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        10.verticalSpace,

        // 搜索
        const MySearchTitle(),

        30.verticalSpace,

        // 热门速读
        Selector<DubanStoreViewModel, List<Book>?>(
          builder: (context, books, child) {
            return MyBookTitle(
              title: '新书速递',
              books: books,
              width: 120.w,
              height: 160.h,
              itemTap: (book) {
                // 跳转到图书详情页
                NavigatorUtils.nav2BookDetailPage(context, book: book);
              },
            );
          },
          selector: (_, viewModel) => viewModel.expressBooks,
        ),

        30.verticalSpace,

        // 一周热门

        Selector<DubanStoreViewModel, List<Book>?>(
          builder: (context, books, child) {
            return MyBookTitle(
              title: '一周热门图书榜',
              books: books,
              width: 120.w,
              height: 160.h,
              showRate: true,
              itemTap: (book) {
                // 跳转到图书详情页
                NavigatorUtils.nav2BookDetailPage(context, book: book);
              },
            );
          },
          selector: (_, viewModel) => viewModel.weekBooks,
        ),

        // top250

        30.verticalSpace,

        Selector<DubanStoreViewModel, List<Book>?>(
          builder: (context, books, child) {
            return MyBookTitle(
              title: '豆瓣阅读Top250',
              books: books,
              width: 120.w,
              height: 160.h,
              itemTap: (book) {
                // 跳转到图书详情页
                NavigatorUtils.nav2BookDetailPage(context, book: book);
              },
            );
          },
          selector: (_, viewModel) => viewModel.top250Books,
        ),
      ],
    ),
  ));
}
